var mongoose = require('mongoose');

//Genre Schema

var faultSchema = new mongoose.Schema({
	title:{
		type: String,
		required: true
	},
	number:{
		type: String,
		required: true
	},
	level:{
		type:String,
		default: "非常严重"
	},
	description:{
		type: String,
	},
	machine_type:{
		type: String,
		default:"CAK63/80/100系列"
	},
	location:{
		type: String,
		default:"主轴"
	},	
	factory:{
		type: String,
		default:"上海机床厂"
	},

	images:
		[{title: String,date:Date}]
	,
	movies:
		[{title: String,date:Date}]
	,
	files:
		[{originalName:String,saveName:String,date:Date}]
	,
	occur_date:{
		type: Date,
		default: Date.now,
		// default:"2016-11-11"
	},
	manu_man_name:{
		type: String,
		default:"谢汝"
	},
	manu_man_email:{
		type: String,
		default:"xieru_sheldon@outlook.com"
	},
	manu_man_tel:{
		type: String,
		default:"15306771856"
	},
	progress:{
		type: Number,
		default: 0.1
	},
	diagnosis:
		[{title:String,text:String,date:Date}]
    ,
    vibrationData:
        [{date:Date,dataList:[Number]}]
    ,
    tempeData:
        [{date:Date,dataList:[{time:Date,value:Number}]}]
    ,


});


faultSchema.methods.addPhoto = function(obj,callback){
	this.images.push(obj);
	this.save();
};

faultSchema.methods.addMovie = function (obj,callback) {
	this.movies.push(obj);
	this.save();
}
faultSchema.methods.addAnaFile = function (obj) {
	this.files.push(obj);
	this.save();
};

faultSchema.methods.addDiagnosis = function (obj,progress) {
	this.diagnosis.push(obj);
	this.progress = progress;
	this.save();
}

faultSchema.methods.addVibrationData = function (obj) {
    this.vibrationData.push(obj);
    this.save();
}

faultSchema.methods.addTempeData = function (obj) {
    this.tempeData.push(obj);
    this.save();
}



var Fault = module.exports = mongoose.model('faults',faultSchema);

//Get Genres 




module.exports.getFaults = function (callback,limit) {
	Fault.find(callback).limit(limit);
}

module.exports.getFaultById = function (id,callback) {
	Fault.findById(id,callback);
}

module.exports.addFault = function (fault,callback) {
	Fault.create(fault,callback);
}

//Update Genre
module.exports.updateFault = function (id,fault,options,callback) {
	var query = {_id:id};
	// var update = {
	// 	title: fault.title,
	// 	number:fault.number,
    //
	// 	description:fault.description,
	// 	machine_type:fault.machine_type,
	// 	location:fault.location,
	// 	factory:fault.factory,
	// 	occur_date:fault.occur_date,
	// 	manu_man_name:fault.manu_man_name,
	// 	manu_man_tel:fault.manu_man_tel,
	// 	imageName:fault.imageName
	// }
	Fault.findOneAndUpdate(query,fault,options,callback);
}


module.exports.deleteFault= function (id,callback) {
	var query = {_id:id};
	Fault.remove(query,callback);
}


module.exports.findFaultByDate = function(beginDate,endDate,callback){
	var query = {
		occur_date:{
		"$gte": new Date(beginDate),
        "$lt":new Date(endDate)
	}
	}

	Fault.find(query,callback);
}



module.exports.findFault = function (req,callback) {


	var query = {};

	if (req.hasOwnProperty('beginTime') && req.hasOwnProperty('endTime') ){
		query['occur_date'] = {
			"$gte": new Date(req.beginTime),
			"$lt":new Date(req.endTime)
		}

	}


	if (req.hasOwnProperty('beginTime')){
		query['occur_date'] = {
			"$gte": new Date(req.beginTime),

		}

		delete  query.beginTime;
	}

	if (req.hasOwnProperty('endTime')){
		query['occur_date'] = {
			"$lt":new Date(req.endTime),
		}

	}


	if (req.title){
		query['title'] = new RegExp(req.title);
	}

	if (req.number){
		query['number'] = new RegExp(req.number);
	}

	if (req.location){
		query['location'] = new RegExp(req.location);
	}

	if (req.factory){
		query['factory'] = new RegExp(req.factory);
	}

	if (req.machine_type){
		query['machine_type'] = new RegExp(req.machine_type);
	}

	if (req.title){
		query['description'] = new RegExp(req.description);
	}





	console.log(query);



	Fault.find(query,callback);
}


module.exports.addImage = function (faultId,imageNames,callback) {

	Fault.findById(faultId,function (err,fault) {
		console.log(fault);
		// fault.addPhoto({"title": imageNames[0],"date":Date.now()});
		for (var i = 0;i < imageNames.length;i++){
			fault.addPhoto({title: imageNames[i],date:Date.now()});
		}
	});
};

module.exports.addMovie = function (faultId,movie,callback) {
	Fault.findById(faultId,function (err,fault) {
		console.log(fault);

		fault.addMovie({title:movie,date:Date.now()});
	})
}

module.exports.addFile = function (faultId,originalNames,saveNames) {
	Fault.findById(faultId,function (err,fault) {

		for (var i=0;i<originalNames.length;i++){
			fault.addAnaFile({originalName:originalNames[i],saveName:saveNames[i],date:Date.now()});
		}
	});

};

module.exports.addDiag = function (faultId,progress,diagText,title) {
	Fault.findById(faultId,function (err,fault) {
		if (err){
			throw err;
		}
		
		var diag = {
		    "title":title,
			"text":diagText,
			"date": Date.now()
		};
		fault.addDiagnosis(diag,progress);

	});

}


module.exports.addVibraData = function (faultId,dataList) {
    Fault.findById(faultId,function (err,fault) {
        if (err){
            throw err;
        }

        var vibraData = {
            "data":dataList,
            "date": Date.now()
        };

        fault.addVibrationData(vibraData);
    });
};


module.exports.addTemperature = function (faultId,temList) {
    // [{date:Date,dataList:[{time:Date,value:Number}]}]

    var tem = {
       "date": Date.now(),
       "dataList":temList
    };


    fault.addTempeData(tem);

}
